local qimou = fk.CreateSkill{
  name = "steam__yuxianquzhi",

}

Fk:loadTranslationTable {
  ["steam__yuxianquzhi"] = "欲先取之",
  [":steam__yuxianquzhi"] = "阶段技，你可以执行X次将X张牌当【顺手牵羊】使用，期间目标角色的手牌数不小于X时均视为【金蝉脱壳】。",
  ["#steam__yuxianquzhi"] = "欲先取之:阶段技，你可以执行X次将X张牌当【顺手牵羊】使用，请选择X。",
  ["qimou_asktouse"] = "请将%arg张牌转化为【顺手牵羊】",
}

--- 询问player是否要发动一个视为技。（额还不太行）
---
--- 如果发动的话，那么会执行一下技能的onUse函数，然后返回选择的牌和目标等。
---@param player ServerPlayer @ 询问目标
---@param params AskToUseActiveSkillParams @ 各种变量
---@return boolean, { cards: integer[], targets: ServerPlayer[], interaction: any }? @ 返回第一个值为是否成功发动，第二值为技能选牌、目标等数据
local function askToUseViewasSkill(room,player, params)
  params.prompt = params.prompt or ""
  params.cancelable = (params.cancelable == nil) and true or params.cancelable
  params.no_indicate = (params.no_indicate == nil) and true or params.no_indicate
  params.extra_data = params.extra_data or Util.DummyTable
  params.skip = params.skip or params.extra_data.skipUse
  ---@diagnostic disable-next-line assign-type-mismatch
  local skill = Fk.skills[params.skill_name] ---@type ActiveSkill | ViewAsSkill


  local command = "AskForUseActiveSkill"
  local data = {params.skill_name, params.prompt, params.cancelable, params.extra_data}

  Fk.currentResponseReason = params.extra_data.skillName
  local req = Request:new(player, command)
  req:setData(player, data)
  req.focus_text = params.extra_data.skillName or params.skill_name
  local result = req:getResult(player)
  Fk.currentResponseReason = nil
  Fk.currentResponsePattern = nil
  if result == "" then
    return false
  end

  data = result
  local card = data.card
  local targets = data.targets
  local card_data = card
  local selected_cards = card_data.subcards
  local interaction
  if not params.no_indicate then
    room:doIndicate(player.id, targets)
  end

  if skill.interaction then
    interaction = data.interaction_data
    skill.interaction.data = interaction
  end

  if skill:isInstanceOf(ActiveSkill) and not params.skip then
    ---@cast skill ActiveSkill
    skill:onUse(room, SkillUseData:new {
      from = player,
      cards = selected_cards,
      tos = table.map(targets, Util.Id2PlayerMapper),
    })
  end
  if skill:isInstanceOf(ViewAsSkill) then
    
      local usecard=skill:viewAs(player, selected_cards)
  
      return {
        cards = usecard,
        targets = table.map(targets, Util.Id2PlayerMapper),
        interaction = interaction
      }
  end

  return true, {
    cards = selected_cards,
    targets = table.map(targets, Util.Id2PlayerMapper),
    interaction = interaction
  }
end

qimou:addEffect("active", {
  anim_type = "offensive",
  prompt = "#steam__yuxianquzhi",
  card_num = 0,
  target_num = 0,
  interaction = function(self, player)
    return UI.Spin {
      from = 1,
      to = 10,
    }
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(qimou.name, Player.HistoryPhase) == 0 
  end,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = effect.from
    local n = self.interaction.data
    for i = 1, n do
      room:setPlayerMark(player,"steam__yuxianquzhi_num",n)
      local  dat = askToUseViewasSkill(room,player, {
        skill_name = "steam__yuxianquzhi_viewas",
        prompt = "qimou_asktouse:::"..n,
        cancelable = true,
        extra_data = {
          yuxianquzhi_num = n,
        }
      })
      if not dat then return end
      local use = {
        from = player,
        tos = dat.targets,
        card = dat.cards,
      }

      if use.tos[1]:getHandcardNum() < n then
        local card = Fk:cloneCard("crafty_escape")
        card.skillName = qimou.name
        card:addSubcards( dat.cards.subcards)
        use.card=card
        use.from=use.tos[1]
        use.tos[1]:chat("金蝉脱壳！")
      end
      player.room:useCard(use)
    end
    room:setPlayerMark(player,"steam__yuxianquzhi_num",0)
  end,
})



return qimou